Event lifecycle
AutoNumeric elements are transparent to the native input
and change
events, which means those are correctly sent when using an <input>
element managed by AutoNumeric.
AutoNumeric custom events
In addition to the native events, custom events sent by AutoNumeric elements allows you to hook into the formatting lifecycle, as you see fit:
'autoNumeric:correctedValue'
when an invalid value is corrected'autoNumeric:initialized'
when the AutoNumeric element is initialized'autoNumeric:invalidFormula'
when the user tries to validate an invalid math expression'autoNumeric:invalidValue'
when an invalid value is entered (ie. when the raw value is out of the min/max range)'autoNumeric:rawValueModified'
when therawValue
is modified'autoNumeric:formatted'
when all the formatting is done and the formatted string is modified'autoNumeric:minExceeded'
if theminimumValue
is not respected'autoNumeric:maxExceeded'
if themaximumValue
is not respected'autoNumeric:validFormula'
when the user validate a valid math expression
Tip
You can also set if the events triggered by the AutoNumeric elements, custom or native, should:
- Bubble up (option
eventBubbles
) or - Be cancelable (option
eventIsCancelable
).
Event lifecycle
Whenever an AutoNumeric element is initialized, the custom 'autoNumeric:initialized'
event is sent.
When using AutoNumeric.multiple()
to initialize numerous elements at once, as many 'autoNumeric:initialized'
events are sent as there are initialized elements.
Finally, the 'change'
event is sent on blur
if the value has been changed since the focus
one.
Note
The AutoNumeric.format()
static function does trigger an 'autoNumeric:formatted'
event if the value that the user is trying to format is outside the minimumValue
and maximumValue
range, with the detail
attribute containing the range error message.
Custom events details
'autoNumeric:formatted'
The 'autoNumeric:formatted'
event has a payload that contains the following detail
attribute:
const theCustomEvent = {
detail : {
oldValue : "78,00 €", // The previous formatted value
newValue : "788,00 €", // The new formatted value
oldRawValue: 78, // The previous raw value
newRawValue: 788, // The new raw value
isPristine : false, // Is the element value still pristine? In other words, has its value changed since its initialization?
error : null, // The error message as a string, `null` if no errors.
aNElement : theAutoNumericObject, // The AutoNumeric object emitting this event
},
// ...and the usual `bubbles` and `cancelable` attributes
}
function onFormattedEvent(event) {
if (!event.detail.isPristine) {
console.log(`The element value has been changed from ${event.detail.oldValue} to ${event.detail.newValue}.`);
}
}
'autoNumeric:rawValueModified'
The 'autoNumeric:rawValueModified'
event has a payload that contains the following detail
attribute:
const theCustomEvent = {
detail : {
oldRawValue: 78, // The previous raw value
newRawValue: 788, // The new raw value
isPristine : false, // Is the `rawValue` still pristine? In other words, did it changed since the object initialization?
error : null, // The error message as a string, `null` if no errors.
aNElement : theAutoNumericObject, // The AutoNumeric object emitting this event
},
// ...
}
'autoNumeric:initialized'
The 'autoNumeric:initialized'
event has a payload that contains the following detail
attribute:
const theCustomEvent = {
detail : {
newValue : "788,00 €", // The new formatted value
newRawValue: 788, // The new raw value
error : null, // The error message as a string, `null` if no errors.
aNElement : theAutoNumericObject, // The AutoNumeric object emitting this event
},
// ...
}
'autoNumeric:invalidFormula'
The 'autoNumeric:invalidFormula'
event has a payload that contains the following detail
attribute:
const theCustomEvent = {
detail : {
formula : '22+35 - (44', // The invalid formula
aNElement: theAutoNumericObject, // The AutoNumeric object emitting this event
},
// ...
}
'autoNumeric:validFormula'
The 'autoNumeric:validFormula'
event has a payload that contains the following detail
attribute:
const theCustomEvent = {
detail : {
formula : '22+35 - (44)', // The valid formula
result : 13, // The math expression result
aNElement: theAutoNumericObject, // The AutoNumeric object emitting this event
},
// ...
}
This can then be used within another script.
For instance, you could listen to that event in a Vue.js component template like so:
<vue-autonumeric
v-on:autoNumeric:formatted.native="funcCall1"
v-on:autoNumeric:rawValueModified.native="funcCall2"
v-on:autoNumeric:initialized.native="funcCall3"
/>
Check out the official vue-autonumeric component for more info
Key inputs & events sent
Below are listed how AutoNumeric react to different types of key inputs.
Inputing numbers and decimal characters
By default a 'normal' printable character input (ie. '2'
or ','
) will result in those events, in that specific order:
'keydown'
'autoNumeric:minExceeded'
or'autoNumeric:maxExceeded'
only if there was a range problem'keypress'
(this is deprecated and will be removed soon)'input'
'keyup'
'autoNumeric:formatted'
when all the formatting is done'autoNumeric:rawValueModified'
when therawValue
is modified
Note
Please check how is structured the payload attached to the event
variable. The event detail provides easy access to the old and new value.
Modifier keys
When inputting a modifier key (ie. Control
), we get:
'keydown'
'keyup'
'autoNumeric:formatted'
'autoNumeric:rawValueModified'
Deleting numbers
If Delete
or Backspace
is entered, the following events are sent:
'keydown'
'input'
'keyup'
'autoNumeric:formatted'
'autoNumeric:rawValueModified'
Validating the field
If Enter
is entered and the value has not changed, the following events are sent:
'keydown'
'keypress'
'keyup'
'autoNumeric:formatted'
'autoNumeric:rawValueModified'
If Enter
is entered and the value has been changed, the following events are sent:
'keydown'
'keypress'
'change'
'keyup'
'autoNumeric:formatted'
'autoNumeric:rawValueModified'
Pasting data
When a paste
is done with the mouse, the following events are sent:
'input'
'keydown'
'input'
'keyup'
'keyup'
'autoNumeric:formatted'
'autoNumeric:rawValueModified'
When a paste
is done with the keyboard shortcut (ie. ctrl+v
), the following events are sent:
'keydown'
'keydown'
'input'
'keyup'
'keyup'
'autoNumeric:formatted'
'autoNumeric:rawValueModified'